<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of LinkGen
 *
 * @author aneto
 */
abstract class LinkGen {

	static private $instances;

	protected $data;

	const PRODUCT = 1;
	const STRUCTURE = 2;
	const PRODUCT_GROUP = 3;
	const NEWS_GROUP = 4;
	const NEWS = 5;
	const RAW_ACTION = 6;
	const GALLERY = 7;
	const SIGN_IN = 8;
	const SCHEDULE = 9;
	const SCREENING = 10;
	const BASKET = 11;
	const SECTION_PRODUCT_GROUP = 12;
	const TIMETABLE = 13;
	const REGISTER = 14;
	const STRUCTURE_ACTIONS = 15;


	abstract public function link($id, $additionalParams = array());
	abstract public function linkParts($id, $additionalParams = array());

    public function  collectionLinks($elements, $additionalParams = array()) {
		$result = array();
		foreach($this->getIds($elements) as $id){
			$result[$id] = $this->link($id, $additionalParams);
		}
		return $result;
	}

	public function specialLink($param){

	}
	
	abstract protected function init();

	protected function setData($data){
		$this->data = $data;
	}


	public function __construct(){
	 sfProjectConfiguration::getActive()->loadHelpers(array('Url'));

		$this->init();
	}

	protected function getData(){
		return $this->data;
	}

	protected function getIds($elements){
		$result = array(-1);

		foreach($elements as $element){
			if(isset($element['id']) && !empty($element['id']))
				$result[] = $element['id'];
		}

		return $result;
	}

	static function getInstance($type){
		if(!isset(self::$instances[$type])){
			switch($type){
				case self::STRUCTURE:
					self::$instances[$type] = new StructureLinkGen();
					break;
				case self::PRODUCT:
					self::$instances[$type] = new ProductLinkGen();
					break;
				case self::PRODUCT_GROUP:
					self::$instances[$type] = new ProductGroupLinkGen();
					break;
				case self::NEWS:
					self::$instances[$type] = new NewsLinkGen();
					break;
				case self::GALLERY:
					self::$instances[$type] = new GalleryLinkGen();
					break;
				case self::SIGN_IN:
					self::$instances[$type] = new SignInLinkGen();
					break;
				case self::SCHEDULE:
					self::$instances[$type] = new ScheduleLinkGen();
					break;
				case self::RAW_ACTION:
					self::$instances[$type] = new RawActionLinkGen();
					break;
				case self::SCREENING:
					self::$instances[$type] = new ScreeningLinkGen();
					break;
			    case self::BASKET:
					self::$instances[$type] = new BasketLinkGen();
					break;
				case self::SECTION_PRODUCT_GROUP:
					self::$instances[$type] = new SectionProductGroupLinkGen();
					break;
				case self::TIMETABLE:
					self::$instances[$type] = new TimeTableLinkGen();
					break;
				case self::REGISTER:
					self::$instances[$type] = new RegisterLinkGen();
					break;
				case self::STRUCTURE_ACTIONS:
					self::$instances[$type] = new StructureActionsLinkGen();
					break;
				default:
					throw new Exception('wrong link type');
					break;
			}
		}


		return self::$instances[$type];
	}

	protected function getCulture(){
		return self::getAction()->getUser()->getCulture();
	}


	protected function getAbbr(){
		return self::getAction()->getRoute()->getObject()->getLang();
	}

	protected function getAction(){
		return sfContext::getInstance()->getController()
					->getAction( sfContext::getInstance()->getRequest()->getParameter('module'), sfContext::getInstance()->getRequest()->getParameter('action') );
	}
}
?>
